﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmoothStreaming.PerformanceTesting
{
    public enum ResponseRating
    {
        Excellent,
        Good,
        Warning,
        Degraded,
        Poor,
        Failed,
        Unknown
    }

    public class ChunkResponseAnalyzer
    {
        private int[] _audioThresholds = { 100, 200, 250, 300 };
        private int[] _videoThresholds = { 100, 300, 450, 600 };

        public ResponseRating RateChunkResponse(SmoothStreamChunk chunk, ChunkResponse response)
        {
            if (response.Elapsed.HasValue == false) { return ResponseRating.Unknown; }

            if (response.ReturnStatusCodeNumeric > 400)
            {
                return ResponseRating.Failed;
            }

            int[] thresholdMeasurement = _videoThresholds;

            if (chunk.IsAudio)
            {
                thresholdMeasurement = _audioThresholds;
            }

            for (int thresholdIndex = 0; thresholdIndex < thresholdMeasurement.Length; thresholdIndex++)
            {
                int responseThreshold = thresholdMeasurement[thresholdIndex];

                if (response.Elapsed.Value.TotalMilliseconds < responseThreshold)
                {
                    return (ResponseRating)thresholdIndex;
                }
            }

            if (response.Elapsed.Value.TotalMilliseconds > thresholdMeasurement.Max())
            {
                return ResponseRating.Poor;
            }

            return ResponseRating.Unknown;
        }
    }
}
